{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 作业1-PM2.5预测\n",
    "\n",
    "## 项目描述\n",
    "\n",
    "* 本次作业的资料是从行政院环境环保署空气品质监测网所下载的观测资料。\n",
    "* 希望大家能在本作业实现 linear regression 预测出 PM2.5 的数值。\n",
    "\n",
    "## 数据集介绍\n",
    "\n",
    "* 本次作业使用丰原站的观测记录，分成 train set 跟 test set，train set 是丰原站每个月的前 20 天所有资料。test set 则是从丰原站剩下的资料中取样出来。\n",
    "* train.csv: 每个月前 20 天的完整资料。\n",
    "* test.csv : 从剩下的资料当中取样出连续的 10 小时为一笔，前九小时的所有观测数据当作 feature，第十小时的 PM2.5 当作 answer。一共取出 240 笔不重複的 test data，请根据 feature 预测这 240 笔的 PM2.5。\n",
    "* Data 含有 18 项观测数据 AMB_TEMP, CH4, CO, NHMC, NO, NO2, NOx, O3, PM10, PM2.5, RAINFALL, RH, SO2, THC, WD_HR, WIND_DIREC, WIND_SPEED, WS_HR。  \n",
    "\n",
    "\n",
    "## 项目要求\n",
    "- 请手动实现 linear regression，方法限使用 gradient descent。\n",
    "- 禁止使用 numpy.linalg.lstsq\n",
    "\n",
    "\n",
    "## 数据准备\n",
    "无\n",
    "\n",
    "## 环境配置/安装\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirror.baidu.com/pypi/simple/\n",
      "Requirement already up-to-date: pandas in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (1.2.5)\n",
      "Requirement already satisfied, skipping upgrade: pytz>=2017.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas) (2019.3)\n",
      "Requirement already satisfied, skipping upgrade: python-dateutil>=2.7.3 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas) (2.8.0)\n",
      "Requirement already satisfied, skipping upgrade: numpy>=1.16.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from pandas) (1.21.0)\n",
      "Requirement already satisfied, skipping upgrade: six>=1.5 in /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages (from python-dateutil>=2.7.3->pandas) (1.15.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install --upgrade pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 下面该你动手啦！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2.5\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import csv\n",
    "print(pd.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4320, 24)\n"
     ]
    }
   ],
   "source": [
    "dataset_dir = 'work/hw1_data'\n",
    "tr_path = os.path.join(dataset_dir, 'train.csv')\n",
    "test_path = os.path.join(dataset_dir, 'test.csv')\n",
    "\n",
    "#读取数据\n",
    "with open(tr_path, 'r', encoding='big5') as fp:\n",
    "    raw_data = list(csv.reader(fp))\n",
    "    raw_data = np.array(raw_data[1:])[:,3:]\n",
    "    \n",
    "    #预处理数据，将rainfall = NR都去除\n",
    "    raw_data[raw_data == 'NR'] = 0\n",
    "    print(raw_data.shape)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### Feature Extration\n",
    "特征提取，将training set的数据进行处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12, 18, 480)\n"
     ]
    }
   ],
   "source": [
    "#将training data 转成 12*18*480   -->  12个月，每个月都是18维特征，包含480个小时的训练数据\r\n",
    "month_data = []\r\n",
    "for month in range(12):\r\n",
    "    sample = np.empty([18, 480])\r\n",
    "    for day in range(20):\r\n",
    "        sample[:,24*day:24*(day+1)] = raw_data[18*(20*month+day):18*(20*month+day+1),:]\r\n",
    "    month_data.append(sample)\r\n",
    "month_data = np.array(month_data)\r\n",
    "print(month_data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5652, 162)\n",
      "(5652, 1)\n",
      "[[14.  14.  14.  ...  2.   2.   0.5]\n",
      " [14.  14.  13.  ...  2.   0.5  0.3]\n",
      " [14.  13.  12.  ...  0.5  0.3  0.8]\n",
      " ...\n",
      " [17.  18.  19.  ...  1.1  1.4  1.3]\n",
      " [18.  19.  18.  ...  1.4  1.3  1.6]\n",
      " [19.  18.  17.  ...  1.3  1.6  1.8]]\n"
     ]
    }
   ],
   "source": [
    "# np.concatenate()\r\n",
    "# magic number 有点多\r\n",
    "x = np.empty([0, 9*18]).astype(float) # 0行是等下用concatenate\r\n",
    "y = np.empty([0, 1]).astype(float)    # 0行是等下用concatenate\r\n",
    "\r\n",
    "post_data = np.empty([0, 18*10])\r\n",
    "for month in range(12):\r\n",
    "    temp = np.empty([0,18*10])\r\n",
    "    month_sample = month_data[month]\r\n",
    "    for hour in range(471):\r\n",
    "        samp = month_sample[:, hour:hour+10]          #每一笔10小时的data,and each hour has 18 dims\r\n",
    "        samp = samp.reshape(1,-1)                     #flatten the data,so each data has 18*10 dims\r\n",
    "        temp_x = np.empty([1,0])\r\n",
    "        for i in range(18):\r\n",
    "            temp_x = np.concatenate((temp_x,samp[:,i*10:i*10+9]), axis=1)\r\n",
    "        x = np.concatenate((x,temp_x), axis=0)\r\n",
    "        # x = np.concatenate((x,samp[:,:18*9]),axis=0)  #10小时里的前9小时\r\n",
    "        y = np.concatenate((y,samp[:,99:100]),axis=0)  #第10小时的pm2.5\r\n",
    "    #     temp = np.concatenate((temp, samp), axis=0)\r\n",
    "    # post_data = np.concatenate((post_data, temp), axis=0)\r\n",
    "print(x.shape)\r\n",
    "print(y.shape)\r\n",
    "#validation is right~\r\n",
    "print(x)\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Standardlization\n",
    "$\\frac{x-x_{mean}}{std}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.35825331, -1.35883937, -1.359222  , ...,  0.26650729,\n",
       "         0.2656797 , -1.14082131],\n",
       "       [-1.35825331, -1.35883937, -1.51819928, ...,  0.26650729,\n",
       "        -1.13963133, -1.32832904],\n",
       "       [-1.35825331, -1.51789368, -1.67717656, ..., -1.13923451,\n",
       "        -1.32700613, -0.85955971],\n",
       "       ...,\n",
       "       [-0.88092053, -0.72262212, -0.56433559, ..., -0.57693779,\n",
       "        -0.29644471, -0.39079039],\n",
       "       [-0.7218096 , -0.56356781, -0.72331287, ..., -0.29578943,\n",
       "        -0.39013211, -0.1095288 ],\n",
       "       [-0.56269867, -0.72262212, -0.88229015, ..., -0.38950555,\n",
       "        -0.10906991,  0.07797893]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_mean = np.mean(x, axis=0)  #得到每一列的均值\r\n",
    "x_std = np.std(x, axis=0)    #得到每一列的标准差\r\n",
    "for row in range(len(x)):\r\n",
    "    for col in range(len(x[0])):\r\n",
    "        if(x_std[col] != 0):\r\n",
    "            x[row][col] = (x[row][col]-x_mean[col])/x_std[col]\r\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Seperate the training data to two set\n",
    "- training set\n",
    "- validation set\n",
    "\n",
    "### 将原来的训练集 8:2 分给training set 和 validation set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4521, 162)\n",
      "(4521, 1)\n",
      "(1131, 162)\n",
      "(1131, 1)\n"
     ]
    }
   ],
   "source": [
    "import math\r\n",
    "x_train = x[:math.floor(len(x)*0.8),:]\r\n",
    "y_train = y[:math.floor(len(y)*0.8),:]\r\n",
    "\r\n",
    "x_validation = x[math.floor(len(x)*0.8):,:]\r\n",
    "y_validation = y[math.floor(len(y)*0.8):,:]\r\n",
    "\r\n",
    "# validate our seperation\r\n",
    "print(x_train.shape)\r\n",
    "print(y_train.shape)\r\n",
    "print(x_validation.shape)\r\n",
    "print(y_validation.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Train the model\n",
    "### model: $y=wx+b$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第0次迭代的loss为： 2580.9961992162957\n",
      "第100次迭代的loss为： 78.23943889269464\n",
      "第200次迭代的loss为： 46.376743751289226\n",
      "第300次迭代的loss为： 39.292012739825005\n",
      "第400次迭代的loss为： 37.03303640572192\n",
      "第500次迭代的loss为： 35.96754963224942\n",
      "第600次迭代的loss为： 35.30326778326181\n",
      "第700次迭代的loss为： 34.82833056115678\n",
      "第800次迭代的loss为： 34.46680651604758\n",
      "第900次迭代的loss为： 34.18229345333888\n",
      "第1000次迭代的loss为： 33.95359160386392\n",
      "第1100次迭代的loss为： 33.76695615568113\n",
      "第1200次迭代的loss为： 33.612905760866774\n",
      "第1300次迭代的loss为： 33.4846213708942\n",
      "第1400次迭代的loss为： 33.37704098996455\n",
      "第1500次迭代的loss为： 33.2863103488316\n",
      "第1600次迭代的loss为： 33.209433259012506\n",
      "第1700次迭代的loss为： 33.14404057811975\n",
      "第1800次迭代的loss为： 33.0882325681637\n",
      "第1900次迭代的loss为： 33.04046821647368\n",
      "第2000次迭代的loss为： 32.99948551691234\n",
      "第2100次迭代的loss为： 32.96424272844093\n",
      "第2200次迭代的loss为： 32.93387421128898\n",
      "第2300次迭代的loss为： 32.90765663202198\n",
      "第2400次迭代的loss为： 32.88498270223538\n",
      "第2500次迭代的loss为： 32.86534049705348\n",
      "第2600次迭代的loss为： 32.84829697820218\n",
      "第2700次迭代的loss为： 32.83348473448079\n",
      "第2800次迭代的loss为： 32.82059121810354\n",
      "第2900次迭代的loss为： 32.80934994077\n",
      "第3000次迭代的loss为： 32.799533225062106\n",
      "第3100次迭代的loss为： 32.7909462019645\n",
      "第3200次迭代的loss为： 32.783421815178116\n",
      "第3300次迭代的loss为： 32.77681664492051\n",
      "第3400次迭代的loss为： 32.77100740315567\n",
      "第3500次迭代的loss为： 32.76588798216555\n",
      "第3600次迭代的loss为： 32.7613669615155\n",
      "第3700次迭代的loss为： 32.757365496514666\n",
      "第3800次迭代的loss为： 32.75381552548045\n",
      "第3900次迭代的loss为： 32.750658244396966\n",
      "第4000次迭代的loss为： 32.74784280658452\n",
      "第4100次迭代的loss为： 32.74532521227047\n",
      "第4200次迭代的loss为： 32.743067358851576\n",
      "第4300次迭代的loss为： 32.74103622745237\n",
      "第4400次迭代的loss为： 32.73920318533223\n",
      "第4500次迭代的loss为： 32.73754338694967\n",
      "第4600次迭代的loss为： 32.73603525918823\n",
      "第4700次迭代的loss为： 32.73466005848951\n",
      "第4800次迭代的loss为： 32.73340148951022\n",
      "第4900次迭代的loss为： 32.73224537648633\n",
      "The min loss = 32.7311896301958 and the iternum = 4999\n"
     ]
    }
   ],
   "source": [
    "dim = 18*9+1   # +1是为了将常数b直接变成w来调\r\n",
    "n = len(x_train) # training set 样本的个数\r\n",
    "w = np.ones([dim, 1])\r\n",
    "lr = 1 # learning rate\r\n",
    "itertime = 5000\r\n",
    "adagrad = np.zeros([dim,1]) #计算adagrad的梯度\r\n",
    "eps = 0.000001\r\n",
    "min_loss = 5000\r\n",
    "iternum = 0\r\n",
    "best_w = np.empty([dim, 1])\r\n",
    "x_train1 = np.concatenate((x_train, np.ones([n,1])), axis=1)\r\n",
    "x_train1.shape\r\n",
    "\r\n",
    "# train the model\r\n",
    "for t in range(itertime):\r\n",
    "    loss = np.vdot(np.dot(x_train1,w)-y_train,np.dot(x_train1,w)-y_train)/n\r\n",
    "    if(t%100 == 0):\r\n",
    "        print(\"第{}次迭代的loss为： {}\".format(t, loss))\r\n",
    "    gradient = 2*np.dot(x_train1.transpose(),np.dot(x_train1,w)-y_train)\r\n",
    "    adagrad += gradient ** 2\r\n",
    "    if(loss < min_loss):\r\n",
    "        min_loss = loss\r\n",
    "        best_w = w\r\n",
    "        iternum = t\r\n",
    "    w = w - lr*gradient/np.sqrt(adagrad+eps)\r\n",
    "\r\n",
    "np.save('data/weight.npy', best_w)\r\n",
    "print('The min loss = {} and the iternum = {}'.format(min_loss, iternum))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Validation the model\n",
    "之前我们将原来的training data 分成了 training set 和 validation set\n",
    "现在用validation set 来验证我们的model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32.095507125265755"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val_n = len(x_validation) # validation set 的长度\r\n",
    "w = np.load('data/weight.npy')\r\n",
    "x_validation1 = np.concatenate((x_validation,np.ones([val_n,1])),axis=1) # 加多一列，消除常数项b的影响\r\n",
    "val_loss = np.vdot(np.dot(x_validation1,w)-y_validation,np.dot(x_validation1,w)-y_validation)/val_n\r\n",
    "val_loss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Predict the PM2.5 on the testing set\n",
    "用我们上面训练到的模型来预测testing set 里面的PM2.5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### Read the testing data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[21. , 21. , 20. , ...,  1. ,  0.6,  1.8],\n",
       "       [14. , 13. , 13. , ...,  1.9,  1.6,  1.2],\n",
       "       [32. , 31. , 32. , ...,  0.3,  0.6,  1.1],\n",
       "       ...,\n",
       "       [25. , 26. , 27. , ...,  2. ,  1.5,  3. ],\n",
       "       [11. , 11. , 11. , ...,  0.6,  0.5,  0.5],\n",
       "       [14. , 14. , 14. , ...,  4.9,  5.2,  3.6]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用csv package 来读取testing data\r\n",
    "with open(test_path, 'r') as fp:\r\n",
    "    test_raw_data = list(csv.reader(fp))\r\n",
    "    test_raw_data = np.array(test_raw_data[:])[:,2:]\r\n",
    "    test_raw_data[test_raw_data=='NR'] = 0\r\n",
    "    test_raw_data.astype(float)\r\n",
    "\r\n",
    "# 把数据集变成 samples*dimensions ==> 240 * (18*9)\r\n",
    "test_data = np.empty([0, 18*9])\r\n",
    "for s in range(240):\r\n",
    "    sam = np.empty([1, 0])\r\n",
    "    temp1 = test_raw_data[18*s:(s+1)*18,:].reshape(1,-1)\r\n",
    "    # sam = np.concatenate((sam, temp1), axis=1).astype(float)\r\n",
    "    for d in range(18):\r\n",
    "        row = s*18+d\r\n",
    "        sam = np.concatenate((sam, test_raw_data[row:row+1,:]),axis=1).astype(float)\r\n",
    "    test_data = np.concatenate((test_data,sam),axis=0)\r\n",
    "test_data.astype(float)\r\n",
    "test_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 对testing set 做 Standardlization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  7.05455949],\n",
       "       [ 18.21744271],\n",
       "       [ 23.91932215],\n",
       "       [  7.11660946],\n",
       "       [ 27.18883517],\n",
       "       [ 22.48330039],\n",
       "       [ 24.29042623],\n",
       "       [ 30.79890901],\n",
       "       [ 16.77350982],\n",
       "       [ 60.12595122],\n",
       "       [ 11.85514653],\n",
       "       [  9.01378864],\n",
       "       [ 62.85350923],\n",
       "       [ 53.66631226],\n",
       "       [ 21.87560653],\n",
       "       [ 12.21719298],\n",
       "       [ 32.3419287 ],\n",
       "       [ 66.25816547],\n",
       "       [ -0.98296759],\n",
       "       [ 17.13044942],\n",
       "       [ 41.70022345],\n",
       "       [ 72.02215514],\n",
       "       [  8.85223553],\n",
       "       [ 17.69603226],\n",
       "       [ 14.71314389],\n",
       "       [ 38.2001902 ],\n",
       "       [ 14.2509967 ],\n",
       "       [ 72.03344682],\n",
       "       [  6.87981293],\n",
       "       [ 55.88531088],\n",
       "       [ 24.46970975],\n",
       "       [  9.25889594],\n",
       "       [  3.31207547],\n",
       "       [ 18.95217052],\n",
       "       [ 28.85610727],\n",
       "       [ 37.02452431],\n",
       "       [ 43.18744663],\n",
       "       [ 30.53467463],\n",
       "       [ 41.4687915 ],\n",
       "       [ 35.47285828],\n",
       "       [  7.10977776],\n",
       "       [ 41.30293446],\n",
       "       [ 31.21654429],\n",
       "       [ 51.32593902],\n",
       "       [ 17.81886088],\n",
       "       [ 35.44610049],\n",
       "       [ 24.072478  ],\n",
       "       [  9.03513912],\n",
       "       [ 26.16377323],\n",
       "       [ 32.11306703],\n",
       "       [ 21.00684413],\n",
       "       [  8.15869403],\n",
       "       [ 21.92955382],\n",
       "       [ 52.8908286 ],\n",
       "       [ 16.71022485],\n",
       "       [ 36.15836574],\n",
       "       [ 32.27727362],\n",
       "       [ 20.82447123],\n",
       "       [ 58.06041836],\n",
       "       [ 22.83369101],\n",
       "       [ 14.77777731],\n",
       "       [ 41.43846568],\n",
       "       [ 12.7212695 ],\n",
       "       [ 48.98928538],\n",
       "       [ 13.19921345],\n",
       "       [ 15.32684493],\n",
       "       [ 14.92418454],\n",
       "       [ -1.10506576],\n",
       "       [ 43.89975794],\n",
       "       [ 30.14881398],\n",
       "       [ 19.83598674],\n",
       "       [ 41.11911197],\n",
       "       [ 61.21554211],\n",
       "       [  5.63402779],\n",
       "       [ 16.29168826],\n",
       "       [  3.74989071],\n",
       "       [ 40.51104203],\n",
       "       [ 14.67700742],\n",
       "       [ 22.39433737],\n",
       "       [ 21.10567603],\n",
       "       [ 24.03108346],\n",
       "       [ 36.18506915],\n",
       "       [ 22.27963858],\n",
       "       [ 90.16827355],\n",
       "       [ 37.81476894],\n",
       "       [ 29.07613731],\n",
       "       [ 22.38917763],\n",
       "       [ 33.07491433],\n",
       "       [ 23.77725825],\n",
       "       [ 19.72738592],\n",
       "       [ 29.14428958],\n",
       "       [ 41.30967923],\n",
       "       [  4.64291567],\n",
       "       [ 38.60117185],\n",
       "       [ 46.16412994],\n",
       "       [ 16.46765996],\n",
       "       [ 32.24680561],\n",
       "       [ 13.23296516],\n",
       "       [ 23.65208945],\n",
       "       [  5.3214703 ],\n",
       "       [ 18.31408884],\n",
       "       [ 27.12637079],\n",
       "       [ 13.52198206],\n",
       "       [ 16.58717723],\n",
       "       [ 25.34035761],\n",
       "       [ 39.41367889],\n",
       "       [ 32.19793684],\n",
       "       [  6.45844861],\n",
       "       [  6.12880043],\n",
       "       [ 77.83989692],\n",
       "       [ 47.19219417],\n",
       "       [ 17.00621988],\n",
       "       [ 28.49757991],\n",
       "       [ 16.21470487],\n",
       "       [ 13.8540935 ],\n",
       "       [ 25.09176861],\n",
       "       [ 26.4673056 ],\n",
       "       [ 10.75881178],\n",
       "       [ 18.24941285],\n",
       "       [ 19.34567965],\n",
       "       [ 81.06464988],\n",
       "       [ 25.76407735],\n",
       "       [ 36.05133427],\n",
       "       [ 25.33873684],\n",
       "       [  7.29743079],\n",
       "       [ 39.04416778],\n",
       "       [  9.82778985],\n",
       "       [ 21.42584455],\n",
       "       [ 29.30863067],\n",
       "       [ 63.09637021],\n",
       "       [ 22.82206047],\n",
       "       [ 22.95918083],\n",
       "       [ 59.17064051],\n",
       "       [ 15.00262294],\n",
       "       [ 13.74317946],\n",
       "       [  3.30358505],\n",
       "       [ 12.54364929],\n",
       "       [ 58.28450278],\n",
       "       [ 19.66909559],\n",
       "       [  4.95227508],\n",
       "       [ 28.58956477],\n",
       "       [ 24.96891992],\n",
       "       [ 43.94669965],\n",
       "       [ 31.13435598],\n",
       "       [ 17.77532289],\n",
       "       [ 25.93463658],\n",
       "       [ 12.00928164],\n",
       "       [ 51.65836521],\n",
       "       [ 22.95178102],\n",
       "       [ 38.5345146 ],\n",
       "       [ 10.48295388],\n",
       "       [  8.23343806],\n",
       "       [ 24.11340323],\n",
       "       [  6.28927013],\n",
       "       [ 15.51174127],\n",
       "       [ 41.04775958],\n",
       "       [  8.2574517 ],\n",
       "       [ 35.86246094],\n",
       "       [ 12.15057658],\n",
       "       [ 19.50028751],\n",
       "       [ 41.7881493 ],\n",
       "       [ 20.15430715],\n",
       "       [ 13.64627761],\n",
       "       [  7.96503186],\n",
       "       [ 54.22559709],\n",
       "       [ 30.87506898],\n",
       "       [ -1.15523819],\n",
       "       [ 17.21083787],\n",
       "       [ 64.10447402],\n",
       "       [ 13.76878267],\n",
       "       [ 64.54309314],\n",
       "       [ 39.88096108],\n",
       "       [ 26.67131398],\n",
       "       [ 20.2687843 ],\n",
       "       [ 61.7044702 ],\n",
       "       [ 24.40552437],\n",
       "       [ 20.81923072],\n",
       "       [ 37.74351535],\n",
       "       [ 12.73483004],\n",
       "       [ 30.9500194 ],\n",
       "       [ 17.00946448],\n",
       "       [ 11.50783647],\n",
       "       [ 55.6150027 ],\n",
       "       [ 46.39020574],\n",
       "       [ 16.08352919],\n",
       "       [ 34.45245298],\n",
       "       [ 26.17283763],\n",
       "       [ 69.52604848],\n",
       "       [ 10.03904575],\n",
       "       [ 59.10575502],\n",
       "       [ 38.13272528],\n",
       "       [ 14.44876106],\n",
       "       [ 28.77270117],\n",
       "       [ -0.60383903],\n",
       "       [ 19.31895398],\n",
       "       [  1.2369454 ],\n",
       "       [ 33.75856345],\n",
       "       [ 10.50425502],\n",
       "       [ 19.05254789],\n",
       "       [ 61.01916817],\n",
       "       [ 25.48891833],\n",
       "       [ 26.06664761],\n",
       "       [ 64.49938008],\n",
       "       [ 10.87245712],\n",
       "       [  9.67747603],\n",
       "       [ 11.62192115],\n",
       "       [  6.92117114],\n",
       "       [  1.88948391],\n",
       "       [122.99121035],\n",
       "       [ 20.35260312],\n",
       "       [ 15.99125179],\n",
       "       [ 13.79362063],\n",
       "       [ 35.7897971 ],\n",
       "       [ 35.97749392],\n",
       "       [ 19.31439288],\n",
       "       [ 34.74322484],\n",
       "       [ 78.88726459],\n",
       "       [  0.6014696 ],\n",
       "       [ 12.90289152],\n",
       "       [ 32.44098457],\n",
       "       [ 15.66610583],\n",
       "       [ 12.75072038],\n",
       "       [113.87428832],\n",
       "       [ 13.04743853],\n",
       "       [ 17.09645021],\n",
       "       [ 63.83755692],\n",
       "       [ 15.54098268],\n",
       "       [ 18.99529887],\n",
       "       [ 10.75402443],\n",
       "       [  5.64383515],\n",
       "       [ 45.85698236],\n",
       "       [ 13.4156988 ],\n",
       "       [ 52.65857045],\n",
       "       [ 43.44115603],\n",
       "       [ 25.25295091],\n",
       "       [ 41.38614881],\n",
       "       [ 68.86585002],\n",
       "       [ 41.54828609],\n",
       "       [ 12.01870889],\n",
       "       [ 18.43453743]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data1 = np.empty([240, 18*9])\r\n",
    "for row in range(len(test_data)):\r\n",
    "    for col in range(len(test_data[0])):\r\n",
    "        if(x_std[col] != 0):\r\n",
    "            test_data1[row][col] = (test_data[row][col]-x_mean[col])/x_std[col]\r\n",
    "test_data1 = np.concatenate((test_data1,np.ones([240,1])),axis=1)\r\n",
    "\r\n",
    "test_data1\r\n",
    "y_test = np.dot(test_data1,best_w)\r\n",
    "y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2.25365180e+01 2.25432413e+01 2.25497877e+01 2.25536801e+01\n",
      " 2.25559802e+01 2.25562279e+01 2.25562456e+01 2.25552548e+01\n",
      " 2.25517162e+01 1.70238854e+00 1.70230007e+00 1.70217622e+00\n",
      " 1.70214084e+00 1.70201699e+00 1.70196391e+00 1.70189314e+00\n",
      " 1.70185775e+00 1.70178698e+00 3.89033970e-01 3.88934890e-01\n",
      " 3.88959660e-01 3.89166667e-01 3.89334749e-01 3.89370134e-01\n",
      " 3.89304671e-01 3.89071125e-01 3.88581033e-01 1.40153928e-01\n",
      " 1.40187544e-01 1.40438783e-01 1.40796178e-01 1.41013800e-01\n",
      " 1.41086341e-01 1.41127035e-01 1.41095188e-01 1.40916490e-01\n",
      " 2.14743454e+00 2.15033616e+00 2.15251238e+00 2.15507785e+00\n",
      " 2.15559094e+00 2.15580326e+00 2.15553786e+00 2.15373319e+00\n",
      " 2.14856688e+00 1.01164897e+01 1.01188606e+01 1.01283793e+01\n",
      " 1.01436837e+01 1.01568117e+01 1.01639597e+01 1.01680290e+01\n",
      " 1.01668790e+01 1.01600318e+01 1.22496461e+01 1.22547063e+01\n",
      " 1.22666136e+01 1.22848195e+01 1.22985846e+01 1.23057856e+01\n",
      " 1.23097311e+01 1.23066879e+01 1.22947983e+01 3.18957006e+01\n",
      " 3.19431175e+01 3.19806440e+01 3.20052371e+01 3.20183652e+01\n",
      " 3.20308917e+01 3.20400389e+01 3.20484784e+01 3.20576610e+01\n",
      " 4.25835103e+01 4.25849257e+01 4.26031493e+01 4.26326964e+01\n",
      " 4.26684360e+01 4.27009908e+01 4.27282378e+01 4.27542463e+01\n",
      " 4.27691083e+01 2.13377565e+01 2.13267870e+01 2.13255485e+01\n",
      " 2.13342180e+01 2.13501415e+01 2.13604034e+01 2.13665959e+01\n",
      " 2.13676575e+01 2.13715499e+01 2.03963199e-01 2.03361642e-01\n",
      " 2.02689314e-01 2.02618542e-01 2.02441614e-01 2.02229299e-01\n",
      " 2.02158528e-01 2.02158528e-01 2.02158528e-01 7.31916136e+01\n",
      " 7.31495046e+01 7.31153574e+01 7.30893489e+01 7.30700637e+01\n",
      " 7.30578556e+01 7.30479476e+01 7.30421090e+01 7.30435244e+01\n",
      " 2.76088110e+00 2.76249115e+00 2.76588818e+00 2.76806440e+00\n",
      " 2.76969214e+00 2.77082449e+00 2.77177990e+00 2.77342534e+00\n",
      " 2.77358457e+00 1.83940198e+00 1.83933121e+00 1.83949045e+00\n",
      " 1.83977353e+00 1.83987969e+00 1.83989738e+00 1.83980892e+00\n",
      " 1.83970276e+00 1.83947275e+00 1.56305839e+02 1.56543100e+02\n",
      " 1.56717197e+02 1.56752937e+02 1.56738075e+02 1.56697558e+02\n",
      " 1.56724098e+02 1.56735067e+02 1.56780538e+02 1.58586323e+02\n",
      " 1.58755113e+02 1.58904972e+02 1.58894285e+02 1.58858192e+02\n",
      " 1.58961872e+02 1.58971957e+02 1.58980096e+02 1.59008227e+02\n",
      " 2.29700991e+00 2.29962845e+00 2.30164544e+00 2.30235315e+00\n",
      " 2.30375088e+00 2.30438783e+00 2.30575018e+00 2.30553786e+00\n",
      " 2.30612173e+00 1.71392427e+00 1.71447275e+00 1.71426044e+00\n",
      " 1.71426044e+00 1.71461430e+00 1.71519816e+00 1.71562279e+00\n",
      " 1.71641897e+00 1.71682590e+00]\n"
     ]
    }
   ],
   "source": [
    "print(x_mean)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 将结果保存到csv文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['id', 'value']\n",
      "['id0', 7.05455948754194]\n",
      "['id1', 18.2174427086696]\n",
      "['id2', 23.919322153524504]\n",
      "['id3', 7.116609461430407]\n",
      "['id4', 27.188835170771238]\n",
      "['id5', 22.483300394363173]\n",
      "['id6', 24.290426231443078]\n",
      "['id7', 30.79890901254681]\n",
      "['id8', 16.773509823091885]\n",
      "['id9', 60.125951223329054]\n",
      "['id10', 11.855146533443234]\n",
      "['id11', 9.013788642253026]\n",
      "['id12', 62.85350923455318]\n",
      "['id13', 53.666312262506594]\n",
      "['id14', 21.875606528574192]\n",
      "['id15', 12.217192982280267]\n",
      "['id16', 32.34192869973489]\n",
      "['id17', 66.25816546591358]\n",
      "['id18', -0.9829675900726151]\n",
      "['id19', 17.130449418996093]\n",
      "['id20', 41.70022344656452]\n",
      "['id21', 72.02215514226093]\n",
      "['id22', 8.852235533993248]\n",
      "['id23', 17.696032261363733]\n",
      "['id24', 14.713143890632843]\n",
      "['id25', 38.20019019542129]\n",
      "['id26', 14.250996702646422]\n",
      "['id27', 72.03344681986972]\n",
      "['id28', 6.879812933645422]\n",
      "['id29', 55.885310876754644]\n",
      "['id30', 24.469709753133102]\n",
      "['id31', 9.258895936158371]\n",
      "['id32', 3.312075472436714]\n",
      "['id33', 18.952170516356635]\n",
      "['id34', 28.85610727412171]\n",
      "['id35', 37.02452430850964]\n",
      "['id36', 43.18744662653738]\n",
      "['id37', 30.534674634255733]\n",
      "['id38', 41.46879149677595]\n",
      "['id39', 35.472858279903924]\n",
      "['id40', 7.109777756392798]\n",
      "['id41', 41.302934455488554]\n",
      "['id42', 31.216544292668384]\n",
      "['id43', 51.32593901757792]\n",
      "['id44', 17.818860878855194]\n",
      "['id45', 35.4461004877232]\n",
      "['id46', 24.072477996946077]\n",
      "['id47', 9.035139117618396]\n",
      "['id48', 26.163773231311204]\n",
      "['id49', 32.113067026496694]\n",
      "['id50', 21.006844130707464]\n",
      "['id51', 8.15869403238503]\n",
      "['id52', 21.92955382209025]\n",
      "['id53', 52.89082859582274]\n",
      "['id54', 16.710224850607176]\n",
      "['id55', 36.158365741504795]\n",
      "['id56', 32.277273620509725]\n",
      "['id57', 20.824471226567116]\n",
      "['id58', 58.06041836048345]\n",
      "['id59', 22.83369101075823]\n",
      "['id60', 14.777777311815562]\n",
      "['id61', 41.438465678467686]\n",
      "['id62', 12.721269495076381]\n",
      "['id63', 48.98928538245666]\n",
      "['id64', 13.19921345246428]\n",
      "['id65', 15.326844926558737]\n",
      "['id66', 14.924184542103976]\n",
      "['id67', -1.1050657599765898]\n",
      "['id68', 43.89975793647122]\n",
      "['id69', 30.148813983061697]\n",
      "['id70', 19.835986744057877]\n",
      "['id71', 41.119111966990985]\n",
      "['id72', 61.21554210710015]\n",
      "['id73', 5.634027793802101]\n",
      "['id74', 16.291688258716647]\n",
      "['id75', 3.749890708637853]\n",
      "['id76', 40.5110420278385]\n",
      "['id77', 14.677007418169804]\n",
      "['id78', 22.394337372902463]\n",
      "['id79', 21.105676034480375]\n",
      "['id80', 24.03108346290842]\n",
      "['id81', 36.185069151015355]\n",
      "['id82', 22.279638578703494]\n",
      "['id83', 90.16827354869949]\n",
      "['id84', 37.814768937809184]\n",
      "['id85', 29.076137314816478]\n",
      "['id86', 22.389177625077327]\n",
      "['id87', 33.07491432963707]\n",
      "['id88', 23.77725824963013]\n",
      "['id89', 19.72738592218449]\n",
      "['id90', 29.14428958350584]\n",
      "['id91', 41.30967923172003]\n",
      "['id92', 4.642915667643955]\n",
      "['id93', 38.601171847364824]\n",
      "['id94', 46.16412993814235]\n",
      "['id95', 16.46765996460988]\n",
      "['id96', 32.24680561399908]\n",
      "['id97', 13.232965164156667]\n",
      "['id98', 23.652089447674413]\n",
      "['id99', 5.321470300355184]\n",
      "['id100', 18.314088841344198]\n",
      "['id101', 27.1263707932024]\n",
      "['id102', 13.521982059678]\n",
      "['id103', 16.587177229504384]\n",
      "['id104', 25.340357608935683]\n",
      "['id105', 39.413678885506314]\n",
      "['id106', 32.19793684282494]\n",
      "['id107', 6.458448611371637]\n",
      "['id108', 6.128800432425686]\n",
      "['id109', 77.83989691515714]\n",
      "['id110', 47.192194170596686]\n",
      "['id111', 17.006219881114344]\n",
      "['id112', 28.497579909491016]\n",
      "['id113', 16.214704874124532]\n",
      "['id114', 13.854093501162453]\n",
      "['id115', 25.09176860824017]\n",
      "['id116', 26.467305604867754]\n",
      "['id117', 10.75881178227596]\n",
      "['id118', 18.249412847475245]\n",
      "['id119', 19.345679648379516]\n",
      "['id120', 81.06464988007066]\n",
      "['id121', 25.764077347051007]\n",
      "['id122', 36.05133427365773]\n",
      "['id123', 25.338736844706922]\n",
      "['id124', 7.297430791597339]\n",
      "['id125', 39.044167777426225]\n",
      "['id126', 9.827789847043586]\n",
      "['id127', 21.42584454889289]\n",
      "['id128', 29.308630671152102]\n",
      "['id129', 63.09637021497835]\n",
      "['id130', 22.822060469490996]\n",
      "['id131', 22.959180833747137]\n",
      "['id132', 59.17064050593002]\n",
      "['id133', 15.002622935252804]\n",
      "['id134', 13.743179457009543]\n",
      "['id135', 3.303585045461432]\n",
      "['id136', 12.543649288020255]\n",
      "['id137', 58.28450277805791]\n",
      "['id138', 19.669095588188245]\n",
      "['id139', 4.952275078633228]\n",
      "['id140', 28.589564774022012]\n",
      "['id141', 24.968919915887902]\n",
      "['id142', 43.94669964680551]\n",
      "['id143', 31.13435597742397]\n",
      "['id144', 17.77532288806882]\n",
      "['id145', 25.934636584964387]\n",
      "['id146', 12.009281636504936]\n",
      "['id147', 51.658365208462406]\n",
      "['id148', 22.95178102360512]\n",
      "['id149', 38.534514600129214]\n",
      "['id150', 10.48295387973464]\n",
      "['id151', 8.233438063356727]\n",
      "['id152', 24.113403225600404]\n",
      "['id153', 6.289270128788216]\n",
      "['id154', 15.51174126690622]\n",
      "['id155', 41.04775958455065]\n",
      "['id156', 8.257451695560935]\n",
      "['id157', 35.86246094138834]\n",
      "['id158', 12.15057658212749]\n",
      "['id159', 19.50028751154888]\n",
      "['id160', 41.788149297263956]\n",
      "['id161', 20.154307152957262]\n",
      "['id162', 13.646277611531]\n",
      "['id163', 7.965031859185636]\n",
      "['id164', 54.225597092546344]\n",
      "['id165', 30.875068975140458]\n",
      "['id166', -1.1552381937662233]\n",
      "['id167', 17.2108378680925]\n",
      "['id168', 64.10447401795335]\n",
      "['id169', 13.768782668777238]\n",
      "['id170', 64.54309314396869]\n",
      "['id171', 39.880961080113856]\n",
      "['id172', 26.67131397608676]\n",
      "['id173', 20.26878429872995]\n",
      "['id174', 61.704470199618584]\n",
      "['id175', 24.405524371268655]\n",
      "['id176', 20.819230717150397]\n",
      "['id177', 37.74351534755093]\n",
      "['id178', 12.734830043343752]\n",
      "['id179', 30.95001939908856]\n",
      "['id180', 17.009464482778565]\n",
      "['id181', 11.507836474803419]\n",
      "['id182', 55.61500269953653]\n",
      "['id183', 46.3902057398212]\n",
      "['id184', 16.083529190183327]\n",
      "['id185', 34.45245297889893]\n",
      "['id186', 26.172837627004]\n",
      "['id187', 69.52604848003104]\n",
      "['id188', 10.039045753547224]\n",
      "['id189', 59.105755023529795]\n",
      "['id190', 38.13272527657239]\n",
      "['id191', 14.448761057730282]\n",
      "['id192', 28.772701174573335]\n",
      "['id193', -0.603839034114408]\n",
      "['id194', 19.31895398014846]\n",
      "['id195', 1.2369454018204493]\n",
      "['id196', 33.75856345205487]\n",
      "['id197', 10.504255016863457]\n",
      "['id198', 19.05254789350098]\n",
      "['id199', 61.01916816589442]\n",
      "['id200', 25.488918330311982]\n",
      "['id201', 26.066647611296776]\n",
      "['id202', 64.49938007655192]\n",
      "['id203', 10.87245711662031]\n",
      "['id204', 9.677476027974663]\n",
      "['id205', 11.621921151633877]\n",
      "['id206', 6.921171137092724]\n",
      "['id207', 1.88948390802409]\n",
      "['id208', 122.99121035157648]\n",
      "['id209', 20.35260312401339]\n",
      "['id210', 15.991251793488107]\n",
      "['id211', 13.793620628978848]\n",
      "['id212', 35.78979709699841]\n",
      "['id213', 35.97749391940425]\n",
      "['id214', 19.314392877744364]\n",
      "['id215', 34.74322484444086]\n",
      "['id216', 78.88726459422143]\n",
      "['id217', 0.6014695990711054]\n",
      "['id218', 12.902891523214443]\n",
      "['id219', 32.440984569966005]\n",
      "['id220', 15.666105825831465]\n",
      "['id221', 12.750720377001718]\n",
      "['id222', 113.87428832059223]\n",
      "['id223', 13.04743852786071]\n",
      "['id224', 17.09645020601377]\n",
      "['id225', 63.83755691927724]\n",
      "['id226', 15.540982678039937]\n",
      "['id227', 18.995298872537212]\n",
      "['id228', 10.754024426939484]\n",
      "['id229', 5.643835146742742]\n",
      "['id230', 45.85698235844954]\n",
      "['id231', 13.41569880141703]\n",
      "['id232', 52.65857044928585]\n",
      "['id233', 43.44115603089616]\n",
      "['id234', 25.252950914031043]\n",
      "['id235', 41.38614881063111]\n",
      "['id236', 68.86585002136472]\n",
      "['id237', 41.548286093030114]\n",
      "['id238', 12.018708894548682]\n",
      "['id239', 18.43453742618621]\n"
     ]
    }
   ],
   "source": [
    "with open('work/submit.csv', mode='w', newline='') as fp:\r\n",
    "    csv_writer = csv.writer(fp)\r\n",
    "    header = ['id', 'value']\r\n",
    "    print(header)\r\n",
    "    csv_writer.writerow(header)\r\n",
    "    for i in range(len(y_test)):\r\n",
    "        row = ['id'+str(i), y_test[i][0]]\r\n",
    "        csv_writer.writerow(row)\r\n",
    "        print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## Craft草稿"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy.concatenate((mat1, mat2, mat3,...), axis=0)   axis=0 在第0维concat\r\n",
    "a = np.zeros([4,1])\r\n",
    "b = np.ones([4,3])\r\n",
    "c = np.concatenate((a,b), axis=1)\r\n",
    "d = np.concatenate((a,b,c), axis=1)\r\n",
    "\r\n",
    "e = np.empty([4,0])\r\n",
    "e = np.concatenate((e,a), axis=1)\r\n",
    "\r\n",
    "a = [1,2,3]\r\n",
    "a[0:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4,  9],\n",
       "       [16, 25, 36]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,2,3],\r\n",
    "                [4,5,6]])\r\n",
    "a\r\n",
    "\r\n",
    "b = np.dot(a,a.transpose())\r\n",
    "b\r\n",
    "\r\n",
    "c = a*a\r\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.5, 2.5])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#a = list([[1,2,3],[4,5,6]])\r\n",
    "a = np.array([[1,2,3,4],[1,2,3,4]])\r\n",
    "np.mean(a, axis=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.0.0b0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
